<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px"
      @submit.native.prevent>
      <el-form-item label="持有人姓名" prop="type">
        <el-input v-model="queryParams.nickName" placeholder="请输入持有人姓名" clearable style="width: 240px" />
      </el-form-item>
      <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
        搜索
      </el-button>
      <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-popover placement="bottom" trigger="click">
          <div>
            <div id="qrCode" ref="qrCode"></div>
            <div class="text-center">打开APP扫一扫添加签名</div>
          </div>
          <el-button slot="reference" type="primary" plain icon="el-icon-plus" size="mini">新建签名</el-button>
        </el-popover>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:article:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="签名" align="center" prop="name" />
      <el-table-column label="签名图片" align="center" prop="content">
        <template slot-scope="scope">
          <img :src="basUrlImg + scope.row.signList" width="100" height="100" />
        </template>
      </el-table-column>
      <el-table-column label="新增时间" align="center" prop="createTime" />
      <el-table-column label="是否默认" align="center" prop="defaultSign">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.defaultSign" active-color="#1F8AFF" inactive-color="#BDBDBD"
            @change="isdefault(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column label="持有人姓名" align="center" prop="nickName" />
      <el-table-column label="持有人账号" align="center" prop="userId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="small" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:article:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
  import QRCode from "qrcodejs2";
  import {
    signList,
    signDelete,
    setDefaultSign
  } from "@/api/message/index";

  export default {
    name: "Article",
    data() {
      return {
        // 遮罩层
        loading: true,
        basUrlImg: process.env.VUE_APP_BASE_API,
        uploadUrl: process.env.VUE_APP_BASE_API + "/common/upload",
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总持有人账号
        total: 0,
        // 表格数据
        dataList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          type: 1,
        },
      };
    },
    computed: {
      user() {
        return this.$store.state.user;
      },
    },
    mounted() {
      this.getList();
      this.createQrcode();
    },
    methods: {
      // 创建二维码
      createQrcode() {
        let data = {
          app: "fire",
          userId: this.user.id,
          page: "picture",
        };
        new QRCode(this.$refs.qrCode, {
          width: 200,
          height: 200, // 高度
          text: JSON.stringify(data),
          correctLevel: QRCode.CorrectLevel.H,
        });
      },
      // 默认切换
      isdefault(i) {
        setDefaultSign({
          defaultSign: i.defaultSign,
          id: i.id,
          type: 1,
        }).then((res) => {
          if (res.code == 200) {
            this.getList()
          }
          console.log(res, 'ddddddddd')
        });
      },
      /** 查询协议列表 */
      getList() {
        this.loading = true;
        signList(this.queryParams).then((response) => {
          this.dataList = response.rows.map((item) => {
            return {
              ...item,
              signList: item.signUrl.split(","),
            };
          });
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.queryParams.nickName = "";
        this.queryParams.pageNum = 1;
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map((item) => item.id);
        this.single = selection.length !== 1;
        this.multiple = !selection.length;
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal
          .confirm("是否确认删除选中签名？")
          .then(() => {
            signDelete(ids).then(() => {
              this.resetQuery();
              this.multiple = !this.multiple;
              this.getList();
              this.$modal.msgSuccess("删除成功");
            });
          })
          .catch(() => {});
      },
    },
  };

</script>
<style lang="scss" scoped>
  .text-center {
    height: 40px;
    line-height: 40px;
    text-align: center;
  }

</style>
